package org.budo.support.lang.util;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import org.budo.support.dao.page.PageModel;

/**
 * @author lmw
 */
public class DesensitizationUtil {
    /**
     * 脱敏
     */
    public static <T> PageModel<T> desensitization(PageModel<T> pageModel, Function<T, T> function) {
        if (null == pageModel) {
            return null;
        }

        List<T> list = desensitization(pageModel.getList(), function);
        pageModel.setList(list);
        return pageModel;
    }

    private static <T> List<T> desensitization(List<T> listIn, Function<T, T> function) {
        if (null == listIn) {
            return listIn;
        }

        List<T> listOut = new ArrayList<T>();
        for (T each : listIn) {
            T apply = function.apply(each);
            listOut.add(apply);
        }

        return listOut;
    }

    public static String desensitization(String password) {
        if (null == password) {
            return null;
        }

        int length = password.length();
        if (length < 3) {
            return RandomUtil.randomNumberString(10) + length;
        }

        return password.charAt(0) + RandomUtil.randomNumberString(length) + password.charAt(length - 1);
    }
}